﻿using GameCreator.Core;
using Gp.Scripts.Utils;
using UnityEngine;
using UnityEngine.Serialization;

namespace Gp.GCX
{
    public class PlayAnimationClipAc : IAction
    {
        
        [Tooltip("是否是玩家")]public bool isPlayer;
        [FormerlySerializedAs("DesCharacter")] public GameObject desCharacter;
        [FormerlySerializedAs("Clip")] public AnimationClip clip;

        /// <summary>
        /// 注意：该方法仅用于执行对应骨骼的动画，如果动画没有办法匹配骨骼，则不会执行。
        /// </summary>
        /// <param name="target"></param>
        /// <param name="actions"></param>
        /// <param name="index"></param>
        /// <returns></returns>
        public override bool InstantExecute(GameObject target, IAction[] actions, int index)
        {
            Animator animator;
            if (desCharacter.GetComponent<Animator>() == null)
            {
                animator = desCharacter.AddComponent<Animator>();
                AnimatorOverrideController tempController = new AnimatorOverrideController {
                    name = "temp"
                };
                animator.runtimeAnimatorController = tempController;
            }else {
                animator = desCharacter.GetComponent<Animator>();
            }

            if (animator == null || clip == null)
            {
                return true;
            }

            
            animator.PlayClip(clip);
            return true;
        }


#if UNITY_EDITOR
        public new static string NAME = "Gp/角色/执行指定动画";
        private const string NODE_TITLE = "让 [{0}] 执行 [{1}]";

        public override string GetNodeTitle()
        {
            string name = desCharacter == null ? "玩家" : desCharacter.name;
            string str = clip == null ? "无" : clip.name;
            return string.Format(NODE_TITLE, name, str);
        }

#endif
    }
}